<script>
function checkAll(){
	$("input:checkbox").attr('checked',true);
	return false;
	}
var employeeLabel= "";
</script>

<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'users-form',
        'enableAjaxValidation' => false,
            ));
    ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model, 'email'); ?>
        <?php echo $form->textField($model, 'email', array('size' => 60, 'maxlength' => 100)); ?>
        <?php echo $form->error($model, 'email'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'real_name'); ?>
        <?php echo $form->textField($model, 'real_name', array('size' => 50, 'maxlength' => 50)); ?>
        <?php echo $form->error($model, 'real_name'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'password'); ?>
        <?php echo $form->passwordField($model, 'password',array('size'=>45)); ?>
        <?php echo $form->error($model, 'password'); ?>
    </div>   
    
    <div class="row">
        <?php echo $form->labelEx($model, 'utype'); ?>
          <?php echo $form->dropDownList($model, 'utype', Useraccess::getTypeList()); ?>
        <?php echo $form->error($model, 'utype'); ?>
    </div>
     <div class="row" id = "rowEmployee">      
        <?php
        echo CHtml::activeLabel($model,'employeeid',array('label'=>'Select Employee'));
        $employeeName = $model->employee!=null?$model->employee->firstname." ".$model->employee->lastname:'';
        $this->widget('zii.widgets.jui.CJuiAutoComplete',array(
            'id'=>'employeeName',
            'name'=>'employeeName',
            'sourceUrl'=>'index.php?r=ajax/searchEmployee',
            'value'=>$employeeName,
            'options'=>array(
                'minLength'=>2,
              'select'=>'js:function(event,ui){
                    $("#employeeName").val(ui.item.label);
                    employeeLabel = ui.item.label;
                    $("#Users_employeeid").val(ui.item.value);        
                }',
                'close'=>'js:function(event,ui){
                    $("#employeeName").val(employeeLabel);
                }'

            ),
        ));
        echo $form->hiddenField($model,'employeeid');
        //echo $form->dropDownList($model, 'custno', $model->getCustomersList(), array());
        echo $form->error($model, 'employeeid');
        ?>
    </div>
    <div class="row" id = "rowCustomer">      
        <?php
        $customerModel = null;
        if(!$model->isNewRecord)
            $customerModel = Customers::model()->findByPk($model->custno);
        echo CHtml::activeLabel($model,'custno',array('label'=>'Select Customer'));
        $lastName = $customerModel!=null?$customerModel->lastname:'';
        $this->widget('zii.widgets.jui.CJuiAutoComplete',array(
            'id'=>'customerName',
            'name'=>'customerName',
            'sourceUrl'=>'index.php?r=ajax/searchCustomer',
            'value'=>$lastName,
            'options'=>array(
                'minLength'=>2,

                'select'=>'js:function(event,ui){
                    $.ajax({
                        url:"index.php?r=ajax/getLocationList",
                        data:{
                            custno:ui.item.value
                        },
                        success:function(json){
                            var data = $.parseJSON(json);
                            $("#customerName").val(ui.item.label);
                            $("#Users_custno").val(ui.item.value);                            
                        }
                    })
                }'
            )
        ));
        echo $form->hiddenField($model,'custno');
        //echo $form->dropDownList($model, 'custno', $model->getCustomersList(), array());
        echo $form->error($model, 'custno');
        ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'deviceid'); ?>
        <?php echo $form->textField($model, 'deviceid', array('size' => 4, 'maxlength' => 4)); ?>
        <?php echo $form->error($model, 'deviceid'); ?>
    </div>
 

    <div class="row privileges-list" id ="plist" style="height: auto;
         <?php if(isset($model->userAccess->type)&&$model->userAccess->type!=0): ?> display:none; <?php endif; ?>
         ">
        <br/>
        <h1>Privileges list</h1>
        <br/>
        <a onclick="checkAll()" href="javascript:void(0);"><b>Check All</b></a>
         <br/>
        <?php
        $root_list = Permissions::model()->findAll("parent=0");
        $all_list = Permissions::model()->findAll();
        $selected = @unserialize($model->userAccess->privilege);
    
        $selected = $selected?$selected :array();

        foreach ($root_list as $root):
            ?>

            <input value="<?= $root->id ?>" id="Users_privileges_4" 
                   <?php if (in_array($root->id, $selected)) : ?> checked="checked" <?php endif; ?>
                   type="checkbox" name="Users[privileges][]">
            <label><?= $root->title ?></label>
            <br clear="both"/>

            <?php
            if ($root->id):
                ?>   <div class="data-model" style="padding-left: 20px;">
                <?php
                $data = array();
                foreach ($all_list as $item) {

                    if ($root->id == $item->parent) {
                        $data+= array($item->id => $item->title);
                    }
                }

                echo CHtml::checkBoxList('Users[privileges]', $selected, $data);
                ?>
                </div>
                <br clear="both"/>
                <?php
            endif;
        endforeach;
        ?>
    </div>

    <div class="row_buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->
<script>
    var utype = $(Users_utype).val();
   if(utype!=0){
       $('#plist').hide();
   } else{
       $('#plist').show();
   }
  if(utype!=3){
       $('#rowCustomer').hide();
        $('#rowEmployee').show();
   } else{
      $('#rowCustomer').show();
      $('#rowEmployee').hide();
   }
  $('#Users_utype').change(function (e) {
  var utype = $(this).val();
   if(utype!=0){
       $('#plist').hide();
   } else{
       $('#plist').show();
   }
   if(utype!=3){
       $('#rowCustomer').hide();
        $('#rowEmployee').show();
   } else{
      $('#rowCustomer').show();
      $('#rowEmployee').hide();
   }
    });
</script>
